//
//  BVHomeFirstTableViewCell.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/10/12.
//

#import "BVHomeFirstTableViewCell.h"
#import "UIImageView+BVWebCache.h"
#import "UIView+BV.h"
#import "BVService.h"
#import <SVProgressHUD.h>
#import "BVHomeFirstCoverView.h"

@interface BVHomeFirstTableViewCell ()


@property (weak, nonatomic) IBOutlet UIButton *favoriteButton;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet BVHomeFirstCoverView *coverView;
@property (weak, nonatomic) IBOutlet UIView *editButtonContainerView;
@property (weak, nonatomic) IBOutlet UIView *shadowView;

@property (nonatomic, strong) BVMetaModel *model;

@end

@implementation BVHomeFirstTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    self.backgroundImageView.layer.cornerRadius = 11;
    self.backgroundImageView.clipsToBounds = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.containerView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.2].CGColor;
    self.containerView.layer.shadowOffset = CGSizeMake(10, 10);
    self.containerView.layer.shadowRadius = 4;
    self.containerView.layer.shadowOpacity = 10;
    
    self.coverView.clipsToBounds = YES;
    self.coverView.layer.cornerRadius = 11;
    self.shadowView.layer.cornerRadius = 11;
    
    self.clipsToBounds = YES;
    self.layer.masksToBounds = NO;
    self.contentView.clipsToBounds = NO;
    self.contentView.layer.masksToBounds = NO;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)configWithModel:(BVMetaModel *)model {
    self.model = model;
    UIImage *image = model.likes ? [UIImage bv_imageNamed:@"video_favorite_icon_home_red" bundle:[BVBundle bundleWithClass:self.classForCoder name:BV_BUNDLE_HOME_SCENE_MODULE]] : [UIImage bv_imageNamed:@"video_favorite_icon_home_white" bundle:[BVBundle bundleWithClass:self.classForCoder name:BV_BUNDLE_HOME_SCENE_MODULE]];
    [self.favoriteButton setImage:image forState:UIControlStateNormal];
    [self.backgroundImageView bv_imageWithUrl:model.cover];
}

- (IBAction)editButtonAct:(UIButton *)sender {
    if (self.editVideoActBlock) {
        self.editVideoActBlock();
    }
}

- (void)setPlaying:(BOOL)playing {
    self.playButton.hidden = playing;
    self.editButtonContainerView.hidden = playing;
    self.coverView.hidden = playing;
}

- (IBAction)downloadButtonAct:(UIButton *)sender {
    [SVProgressHUD showProgress:0];
    [[BVService shareInstance] downloadVideoWithUrl:self.model.video progress:^(NSProgress * _Nonnull progress) {
        [SVProgressHUD showProgress:(double)progress.completedUnitCount / (double)progress.totalUnitCount];
    } complete:^(BOOL success, NSURL * _Nullable filePath) {
        [SVProgressHUD dismiss];
        if (success) {
            NSString *filePathStr = [filePath.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
            UISaveVideoAtPathToSavedPhotosAlbum(filePathStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        }
    }];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败 请重试"];
    }
    else {
        [SVProgressHUD showSuccessWithStatus:@"保存视频成功"];
    }
  
}

- (IBAction)favoriteButtonAct:(UIButton *)sender {
    self.favoriteAct ? self.favoriteAct(): nil;
}

- (IBAction)shareButtonAct:(UIButton *)sender {
    self.shareActBlock?self.shareActBlock():nil;
}

- (IBAction)playButtonAct:(UIButton *)sender {
    self.playBlock ? self.playBlock(): nil;
}

+ (CGFloat)cellHeight {
    return kSCREEN_WIDTH * 1.536 + 42;
}

@end
